import 'package:amap_base/amap_base.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';

import 'routes.dart';

///启动
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  ///初始化并配置路由
  Routes.configureRoutes(Router());
  _initPermission();
  AMap.init('efd45977550c22be6c04aa96932d4cd9');
  runApp(MaterialApp(onGenerateRoute: Routes.router.generator));

//  runAutoSizeApp(
//    MaterialApp(onGenerateRoute: Routes.router.generator),
//    width: 375,
//    height: 667,
//  );
}

_initPermission() async {
  Map<PermissionGroup, PermissionStatus> permissions =
      await PermissionHandler().requestPermissions([
    PermissionGroup.location,
//    PermissionGroup.sensors,
//    PermissionGroup.speech,
//    PermissionGroup.storage,

  ]);
//  PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.contacts);
}

/// 根界面 决定跳转的页面
class PageRoot extends StatefulWidget {
  PageRoot({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => PageRootState();
}

class PageRootState extends State<PageRoot> {
  String result;

  // Flutter主动发送参数给原生
  static const platform = const MethodChannel("getData4Flutter");

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 0), () {
//      getData4Flutter('getData');
      intentMain();
    });
  }

  Future<Null> getData4Flutter(String method) async {
    try {
      result = await platform.invokeMethod(method);
      intentMain();
    } on PlatformException {}
    setState(() {
      print("dart-setState");
    });
  }

  intentMain() async {
//    String route = '${Routes.login}?pageIndex=$result';
//    Routes.router.navigateTo(context, route,
//        replace: true, transition: TransitionType.fadeIn);
    String route = '${Routes.register}';
    Routes.router.navigateTo(context, route,
        replace: true, transition: TransitionType.fadeIn);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      color: Colors.transparent,
    );
  }
}
